<!--
 * @Author: wangshuguang
 * @Date: 2022-10-19 13:52:14
 * @LastEditTime: 2022-11-01 11:57:13
 * @LastEditors: wangshuguang
 * @Description: 列表批量修改可看权限
-->

<template>
  <el-dialog
    :title="`批量修改可看范围（已选${selectRow.length}）`"
    :visible.sync="dialogVisible"
    width="30%"
    @open="dialogOpen"
  >
    <el-form ref="form" :model="form" size="small" :rules="rules" label-width="120px">
      <SelectRole v-model="agtRadio" :form="form" />
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" size="small" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SelectRole from './SelectRole.vue'
export default {
  components: {
    SelectRole
  },
  props: {
    value: {},
    // 列表选中数据
    selectRow: {}
  },
  data() {
    return {
      rules: {
        agtList: [{ validator: this.validRule, required: true, trigger: ['blur', 'change'] }],
        roleNm: [{ validator: this.validRule, required: true, trigger: ['blur', 'change'] }]
      },
      agtRadio: 1,
      form: {
        agtList: [],
        roleNm: []
      }
    }
  },
  computed: {
    dialogVisible: {
      get() { return this.value },
      set(val) {
        this.$emit('input', val)
      }

    }
  },
  methods: {
    dialogOpen() {
      this.agtRadio = 1
      this.form = {
        agtList: [],
        agtListObj: [],
        roleNm: []
      }
    },
    submit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.$confirm('保存后将覆盖原内容可看范围，是否继续保存?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.$emit('submit', this.form)
          })
        }
      })
    },
    validRule(rule, value, callback) {
      const { field } = rule
      if (field == 'agtList') {
        console.log('validRule', this.form, this.agtRadio)
        if (this.agtRadio == 2 && !this.form.agtList.length) {
          callback('请选择机构')
        } else {
          callback()
        }
      } else if (field == 'roleNm') {
        if (this.agtRadio == 2 && (!this.form.roleNm || !this.form.roleNm.length)) {
          callback('至少选择一个范围')
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
  }
}
</script>

<style>

</style>
